
// Vehicle 类
class Vehicle {
  constructor(brand, year) {
      this.brand = brand;
      this.year = this.validateYear(year);
      Vehicle.incrementVehicleCount();
  }

  validateYear(year) {
      const currentYear = new Date().getFullYear();
      if (year > currentYear) {
          throw new Error('Year cannot be in the future.');
      }
      return year;
  }

  getinfo() {
      return `${this.brand} (${this.year})`;
  }

  static vehicleCount = 0;

  static incrementVehicleCount() {
      Vehicle.vehicleCount++;
  }
}
// Car 类
class Car extends Vehicle {
  constructor(brand, year, mileage) {
      super(brand, year);
      this._mileage = mileage;
  }

  get mileage() {
      return this._mileage;
  }

  set mileage(value) {
      this._mileage = value;
  }

  drive(distance) {
      this.mileage += distance;
  }

  getinfo() {
      return `${super.getinfo()} - Mileage: ${this.mileage}`;
  }

  static compareMileage(car1, car2) {
      return car1.mileage > car2.mileage ? car1 : car2;
  }
}
// 创建两个 Car 实例
const car1 = new Car('Toyota', 2020, 10000);
const car2 = new Car('Honda', 2021, 15000);

// 使用 drive 方法增加其中一辆车的里程数
car1.drive(500);

// 使用 compareMileage 方法比较两辆车的里程数
const higherMileageCar = Car.compareMileage(car1, car2);
console.log(`The car with higher mileage is: ${higherMileageCar.getinfo()}`);

// 调用静态方法 incrementVehicleCount ，增加车辆实例的数量，并打印当前车辆数量
Vehicle.incrementVehicleCount();
console.log(`Current vehicle count: ${Vehicle.vehicleCount}`);
